/*********************************************************************
 * Filename:      goblin_sqs_queue_center.h
 *                
 * It belongs to goblin-os.
 *                
 * Version:       
 * Author:        Ins <InsFous@gmail.com>
 * Created at:    Fri Dec 10 13:50:12 2010
 * Description:   
 *                
 ********************************************************************/
#ifndef _GOBLIN_SQS_QUEUE_CENTER_H_
#define _GOBLIN_SQS_QUEUE_CENTER_H_

#include "thrift/Queue.h"
#include "goblin_sqs_queue.h"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <inttypes.h>
#include <string>
#include <vector>
#include <map>

using goblin::sqs::thrift::QueueName;
using goblin::sqs::thrift::Message;
using goblin::sqs::Queue;
using goblin::sqs::thrift::QueueFlag;
using goblin::sqs::thrift::QueueStatus;

namespace goblin {
  namespace sqs {
    struct QueueDesc {
      Queue *queue;
      QueueFlag::type queue_flag;
    };
    struct QueueDisk {
      QueueFlag::type queue_flag;
      uint32_t queue_name_len;
      char queue_name[0];
    };
    typedef std::map<QueueName, struct QueueDesc> QueueStorage;
    class QueueCenter {
    public:
      QueueCenter(const std::string &db_dir);
      ~QueueCenter();
      bool CreateQueue(const QueueName &queue_name, const QueueFlag::type queue_flag);
      void DeleteQueue(const QueueName &queue_name);
      bool SendMessage(const QueueName &queue_name, const std::string &msg_content, const int32_t msg_type);
      bool FetchMessage(const QueueName &queue_name, Message &msg);
      int FetchMessageList(const QueueName &queue_name, int count, std::vector<Message> &msg_list);
      void GetQueueStatus(QueueStatus &_return, const QueueName &queue_name);
      bool SetQueueCapacity(const QueueName &queue_name, const int64_t capacity);
      void SetQueueFilter(const QueueName &queue_name, const int32_t msg_type);
      void RestoreQueueDefaultFilter(const QueueName &queue_name);
      void SyncToDisk();
      void Save();
    private:
      void Init();
    private:
      QueueStorage all_queues_;
      std::string db_dir_;
      pthread_rwlock_t rw_lock_;
    };      
  }
}




#endif  // _GOBLIN_SQS_QUEUE_CENTER_H_
